<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>bucket_sort 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-pipeline-bucket-sort-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-pipeline-bucket-sort-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-pipeline-bucket-sort-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-pipeline-bucket-sort-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-pipeline-bucket-sort-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-pipeline.html">管道聚合</span>
»
<span class="breadcrumb-node">bucket_sort 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-pipeline-bucket-selector-aggregation.html">« bucket_selector 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-pipeline-avg-bucket-aggregation.html">avg_bucket 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-pipeline-bucket-sort-aggregation"></a>桶排序(bucket_sort)聚合
</h2>
</div></div></div>
<p>
对其父多桶聚合的桶进行排序的父管道聚合。

可以指定零个或多个排序字段以及相应的排序顺序。

每个桶可以基于其<code class="literal">_key</code>、<code class="literal">_count</code>或其子聚合进行排序。

此外，可以参数<code class="literal">from</code>和<code class="literal">size</code>，以便截断结果桶。
</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>
与所有管道聚合一样，<code class="literal">bucket_sort</code>聚合在所有其他非管道聚合之后执行。

这意味着排序只适用于已经从父聚合返回的任何桶。

例如，如果父聚合是<code class="literal">terms</code>聚合，并其参数<code class="literal">size</code>设置为<code class="literal">10</code>，那么<code class="literal">bucket_sort</code>将只对这 10 个返回的词项桶进行排序。
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_syntax_4"></a>语法
</h3>
</div></div></div>
<p>一个单独的<code class="literal">bucket_sort</code>看起来像这样：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "bucket_sort": {
        "sort": [
            {"sort_field_1": {"order": "asc"}},<a id="CO255-1"></a><i class="conum" data-value="1"></i>
            {"sort_field_2": {"order": "desc"}},
            "sort_field_3"
        ],
        "from": 1,
        "size": 3
    }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO255-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
这里，<code class="literal">sort_field_1</code>是要用作主要排序的变量的桶路径，其顺序是升序(asc)。
</p>
</td>
</tr>
</table>
</div>
<div class="table">
<a id="bucket-sort-params"></a>
<p class="title"><strong>表 9. <code class="literal">bucket_sort</code>参数</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="bucket_sort Parameters">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">参数名称</th>
<th align="left" valign="top">描述</th>
<th align="left" valign="top">是否必需</th>
<th align="left" valign="top">默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">sort</code></p></td>
<td align="left" valign="top"><p>
排序所依据的字段列表。有关更多详细信息请参见<a class="xref" href="search-request-body.html#request-body-search-sort" title="排序" rel="nofollow"><code class="literal">排序(sort)</code></a>。
</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">from</code></p></td>
<td align="left" valign="top"><p>给定的值之前位置的桶将被截断</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p><code class="literal">0</code></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">size</code></p></td>
<td align="left" valign="top"><p>要返回的桶数。默认为父聚合的所有桶。</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">gap_policy</code></p></td>
<td align="left" valign="top"><p>在数据中发现间隙时应用的策略(更多详情请参考<a class="xref" href="search-aggregations-pipeline.html#gap-policy" title="Dealing with gaps in the data">处理数据中的间隙</a>)</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p><code class="literal">skip</code></p></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>下面这个代码片段按降序返回与总销售额最高的3个月相对应的桶：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "calendar_interval" : "month"
            },
            "aggs": {
                "total_sales": {
                    "sum": {
                        "field": "price"
                    }
                },
                "sales_bucket_sort": {
                    "bucket_sort": {
                        "sort": [
                          {"total_sales": {"order": "desc"}}<a id="CO256-1"></a><i class="conum" data-value="1"></i>
                        ],
                        "size": 3<a id="CO256-2"></a><i class="conum" data-value="2"></i>
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/551.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO256-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">sort</code>设置为按降序使用<code class="literal">total_sales</code>的值</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO256-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">size</code>设置为3，表示将只返回<code class="literal">total_sales</code>中的前3个月</p>
</td>
</tr>
</table>
</div>
<p>响应可能像下面这样：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
   "took": 82,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "total_sales": {
                   "value": 550.0
               }
            },
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "total_sales": {
                   "value": 375.0
               },
            },
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2,
               "total_sales": {
                   "value": 60.0
               },
            }
         ]
      }
   }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_truncating_without_sorting"></a>不排序截断
</h3>
</div></div></div>
<p>
也可以使用这种聚合来截断结果桶，而不进行任何排序。

为此，只需使用<code class="literal">from</code>和/或<code class="literal">size</code>参数，无需指定<code class="literal">sort</code>。
</p>
<p>下面这个例子简单地截断结果，只返回第二个桶：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "calendar_interval" : "month"
            },
            "aggs": {
                "bucket_truncate": {
                    "bucket_sort": {
                        "from": 1,
                        "size": 1
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/552.console"></div>
<p>响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2
            }
         ]
      }
   }
}</pre>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-pipeline-bucket-selector-aggregation.html">« bucket_selector 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-pipeline-avg-bucket-aggregation.html">avg_bucket 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>